﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Jacobi.Vst.Core;
using Jacobi.Vst.Framework;
using Jacobi.Vst.Framework.Common;
using Aleator.Core;
using Aleator.Resources;

namespace Aleator
{    /// <summary>
    /// Implements the custom UI editor for the plugin.
    /// </summary>
    public class PluginEditor : IVstPluginEditor
    {
        private Plugin _plugin;
        //private WinFormsControlWrapper<AleatorUI> _uiWrapper = new WinFormsControlWrapper<AleatorUI>();

        private WpfControlWrapper<AleatorControl> _uiWrapper =
            new WpfControlWrapper<AleatorControl>(Convert.ToInt32(AppResources.WindowWidth), Convert.ToInt32(AppResources.WindowHeight));

        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        /// <param name="plugin">Must not be null.</param>
        public PluginEditor(Plugin plugin)
        {
            _plugin = plugin;
        }

        /// <summary>
        /// Public accessor so we can play when transport starts
        /// </summary>
        public WpfControlWrapper<AleatorControl> UIWrapper
        {
            get
            {
                return _uiWrapper;
            }
        }

        #region IVstPluginEditor Members

        /// <summary>
        /// Display area bounds
        /// </summary>
        public Rectangle Bounds
        {
            get { return new Rectangle(0, 0, Convert.ToInt32(AppResources.WindowWidth), Convert.ToInt32(AppResources.WindowHeight)); }
        }

        /// <summary>
        /// Close Aleator control
        /// </summary>
        public void Close()
        {
            _uiWrapper.Close();
        }

        /// <summary>
        /// Dont even know what this is for
        /// </summary>
        /// <param name="ascii"></param>
        /// <param name="virtualKey"></param>
        /// <param name="modifers"></param>
        public void KeyDown(byte ascii, VstVirtualKey virtualKey, VstModifierKeys modifers)
        {
            // no-op
        }

        /// <summary>
        /// Dont even kow what this is for
        /// </summary>
        /// <param name="ascii"></param>
        /// <param name="virtualKey"></param>
        /// <param name="modifers"></param>
        public void KeyUp(byte ascii, VstVirtualKey virtualKey, VstModifierKeys modifers)
        {
            // no-op
        }

        /// <summary>
        /// Dont knwo what this is for
        /// </summary>
        public VstKnobMode KnobMode { get; set; }

        /// <summary>
        /// Open Aleator control
        /// </summary>
        /// <param name="hWnd"></param>
        public void Open(IntPtr hWnd)
        {
            _uiWrapper.Open(hWnd);
            _uiWrapper.Instance.PluginProcessor = _plugin.PluginProcessor;
            _uiWrapper.Instance.AddSessionBindings();
        }

        /// <summary>
        /// Process
        /// </summary>
        public void ProcessIdle()
        {
            _uiWrapper.Instance.ProcessIdle();
        }

        #endregion
    }
}
